//! DSL调试测试

use ai_test_platform::dsl::{DSLParser, TemplateEngine, VariableManager};
use serde_json::Value;

#[tokio::test]
async fn debug_variable_resolution() {
    // 1. 创建变量管理器并设置变量
    let mut var_manager = VariableManager::new();
    var_manager.set_global_variable("user_id".to_string(), Value::Number(123.into()));
    var_manager.set_global_variable("base_url".to_string(), Value::String("https://api.example.com".to_string()));
    var_manager.set_global_variable("api_version".to_string(), Value::String("v1".to_string()));
    
    // 2. 创建模板引擎
    let mut template_engine = TemplateEngine::new();
    
    // 3. 测试简单变量解析
    let simple_template = "${user_id}";
    let result1 = template_engine.resolve_string(simple_template, &mut var_manager).unwrap();
    println!("简单变量解析: '{}' -> '{}'", simple_template, result1);
    assert_eq!(result1, "123");
    
    // 4. 测试复杂URL解析
    let url_template = "${base_url}/${api_version}/users/${user_id}";
    let result2 = template_engine.resolve_string(url_template, &mut var_manager).unwrap();
    println!("URL模板解析: '{}' -> '{}'", url_template, result2);
    assert_eq!(result2, "https://api.example.com/v1/users/123");
    
    // 5. 测试JSON值解析
    let json_template = serde_json::json!({
        "url": "${base_url}/${api_version}/users/${user_id}",
        "user_id": "${user_id}"
    });
    
    let result3 = template_engine.resolve_value(&json_template, &mut var_manager).unwrap();
    println!("JSON解析结果: {}", serde_json::to_string_pretty(&result3).unwrap());
    
    assert_eq!(result3["url"].as_str().unwrap(), "https://api.example.com/v1/users/123");
    assert_eq!(result3["user_id"], Value::Number(123.into()));
    
    println!("✅ 变量解析调试测试通过！");
}

#[tokio::test]
async fn debug_dsl_parser() {
    let parser = DSLParser::new();
    
    let yaml_content = r#"
config:
  name: "调试测试"
  base_url: "https://api.example.com"
  variables:
    user_id: 123
    api_version: "v1"

teststeps:
  - name: "测试步骤"
    request:
      method: "GET"
      url: "${base_url}/${api_version}/users/${user_id}"
"#;

    let definition = parser.parse_yaml(yaml_content).expect("解析YAML失败");
    println!("解析的配置: {:?}", definition.config);
    println!("解析的步骤: {:?}", definition.teststeps[0]);
    
    // 创建变量管理器
    let mut var_manager = VariableManager::new();
    
    // 设置全局变量
    if let Some(variables) = &definition.config.variables {
        var_manager.set_global_variables(variables.clone());
    }
    if let Some(base_url) = &definition.config.base_url {
        var_manager.set_global_variable("base_url".to_string(), Value::String(base_url.clone()));
    }
    
    // 创建模板引擎并解析请求
    let mut template_engine = TemplateEngine::new();
    let step = &definition.teststeps[0];
    
    if let Some(request) = &step.request {
        println!("原始请求: {}", serde_json::to_string_pretty(request).unwrap());
        
        let resolved_request = template_engine.resolve_value(request, &mut var_manager).unwrap();
        println!("解析后请求: {}", serde_json::to_string_pretty(&resolved_request).unwrap());
        
        let expected_url = "https://api.example.com/v1/users/123";
        let actual_url = resolved_request["url"].as_str().unwrap();
        
        println!("期望URL: {}", expected_url);
        println!("实际URL: {}", actual_url);
        
        assert_eq!(actual_url, expected_url);
    }
    
    println!("✅ DSL解析器调试测试通过！");
}

#[tokio::test]
async fn debug_function_calls() {
    let mut var_manager = VariableManager::new();
    var_manager.set_global_variable("username".to_string(), Value::String("john_doe".to_string()));

    let mut template_engine = TemplateEngine::new();

    // 测试简单的concat函数调用
    let template1 = "${concat(username, _updated)}";
    let result1 = template_engine.resolve_string(template1, &mut var_manager);
    println!("模板1: '{}' -> {:?}", template1, result1);

    // 测试带引号的参数
    let template2 = "${concat(username, \"_updated\")}";
    let result2 = template_engine.resolve_string(template2, &mut var_manager);
    println!("模板2: '{}' -> {:?}", template2, result2);

    // 测试多个参数
    let template3 = "${concat(\"Hello \", username, \" updated\")}";
    let result3 = template_engine.resolve_string(template3, &mut var_manager);
    println!("模板3: '{}' -> {:?}", template3, result3);

    println!("✅ 函数调用调试测试完成！");
}
